#include <iostream>
#include <cstdio>

using namespace std;

int a[32770];

int min(int a, int b){
    if(a <= b)  return a;
    else return b;
}

int abs(int a){
    if(a == 0)  return 0;
    if(a < 0)   return -a;
    else return a;
}

int main(){
    int n;
    cin >> n;
    int ans = 0;
    int tmp;
    for(int i = 1; i <= n; i++){
        scanf("%d", &a[i]);
        if(i == 1){  
            ans += a[i];
        }else{
            tmp = 5000000;
            for(int j = i - 1; j >= 1; j--){
                tmp = min(tmp, abs(a[i] - a[j]));
                //cout << tmp << "\n";
            }
            //cout << tmp << "\n";
            ans += tmp;
            tmp = 5000000;
        }
    }
    cout << ans;
    return 0;
}